/**
 * Created by Administrator on 2016/12/13.
 */

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EncodingUtils;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;

    public class http {
        /**
         * get 方法
         */
        public static String get(String url){
            String result = "";
            try {
                //获取httpclient实例
                RequestConfig requestConfig=RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
                CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
                //获取方法实例。GET
                HttpGet httpGet = new HttpGet(url);
                httpGet.addHeader("Accept","application/jason,txt/javascript,*/*,q=0.01");
                httpGet.addHeader("Accept-Encoding","gzip,deflate,sdch");
                httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.8");
                httpGet.setHeader("Connection","keep-alive");
                httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36)");
                httpGet.addHeader("X-Requested-With","XMLHttpRequest");
                //执行方法得到响应
                CloseableHttpResponse response = httpclient.execute(httpGet);
                try {
                    //如果正确执行而且返回值正确，即可解析
                    if (response != null
                            && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {


                        //从输入流中解析结果

                       result = EntityUtils.toString(response.getEntity(),"utf-8");
                    }
                } finally {
                    httpclient.close();
                    response.close();
                }
            }catch (Exception e){
                System.out.println("time out");
                e.printStackTrace();
            }
            return result;
        }

        /**
         * stream读取内容，可以传入字符格式
         */
        private static String readResponse(HttpEntity resEntity, String charset) {
            StringBuffer res = new StringBuffer();
            BufferedReader reader = null;
            try {
                if (resEntity == null) {
                    return null;
                }

                reader = new BufferedReader(new InputStreamReader(
                        resEntity.getContent(), charset));
                String line = null;

                while ((line = reader.readLine()) != null) {
                    res.append(line);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                }
            }
            return res.toString();
        }
       public static  void main(String[] args){
           String out =get("http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=jQuery&type=jsonp&tid=24&pn1000");
           System.out.print(out);
       }
    }

